package com.xizimall.rest.service.impl;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.xizimall.mapper.TbContentMapper;
import com.xizimall.pojo.TbContent;
import com.xizimall.pojo.TbContentExample;
import com.xizimall.pojo.TbContentExample.Criteria;
import com.xizimall.rest.dao.JedisClient;
import com.xizimall.rest.service.ContentService;
import com.xizimall.common.utils.JsonUtils;
/**
 * 内容服务
 * <p>Title: ContentServiceImpl</p>
 * <p>Description: </p>
 * <p>华南理工大学研究生</p> 
 * @author	李隆熹
 * @date	2018年4月18日下午9:55:30
 * @version 1.0
 */
@Service
public class ContentServiceImpl implements ContentService {
     
	@Autowired
	private TbContentMapper contentMapper;
	
	@Autowired
	private JedisClient jedisClient;
	
	@Value("${INDEX_CONTENT_REDIS_KEY}")
	private String INDEX_CONTENT_REDIS_KEY;
	
	
	@Override
	public List<TbContent> getContentlist(Long contentCid) {
		//从缓存中区内容
		try {
			String result=jedisClient.hget(INDEX_CONTENT_REDIS_KEY, contentCid+"");
			if (!StringUtils.isBlank(result)){
				//把字符串转换成list
				List<TbContent> resultList=	JsonUtils.jsonToList(result, TbContent.class);
				return resultList;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		TbContentExample example=new TbContentExample();
		Criteria criteria=example.createCriteria();
		criteria.andCategoryIdEqualTo(contentCid);
		List<TbContent> list=contentMapper.selectByExample(example);
		
		//像缓存中添加内容
		try {
			//把list转换成字符串
			String cacheString = JsonUtils.objectToJson(list);
			jedisClient.hset(INDEX_CONTENT_REDIS_KEY, contentCid + "", cacheString);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}

}
